package com.lq.common.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lq.common.Mapper.UserInfoMapper;
import com.lq.common.entity.ShortPage;
import com.lq.common.entity.UserInfo;
import com.lq.common.param.ResponseParam;
import com.lq.common.param.request.UserInfoReq;
import com.lq.common.service.UserInfoService;
import com.lq.common.until.GetUntile;
import com.lq.common.until.Pages;
import com.lq.common.until.Send;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;

/**
 * (UserInfo)表服务实现类
 *
 * @author makejava
 * @since 2023-07-17 22:06:52
 */
@Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoService{

    @Autowired
    private UserInfoMapper userInfoMapper;

    GetUntile untile = new GetUntile();
    Send send = new Send();

    @Override
    public String insert(UserInfo userInfo) throws ParseException {
        userInfo.setCreateTime(untile.getDateTime());
        String resp = Pages.api("https://api.vvhan.com/api/getIpInfo?ip="+userInfo.getIp(),"GET");
        System.out.println(resp);
        JSONObject jsonObject = JSONUtil.parseObj(resp);
        JSONObject infoObj = JSONUtil.parseObj(jsonObject.get("info"));
        userInfo.setCity(infoObj.get("city").toString());
        userInfo.setRegionName(infoObj.get("prov").toString());
        userInfo.setLanguage(infoObj.get("lsp").toString());
        if(userInfoMapper.insert(userInfo) == 1){
         /*   String subject = "系统访问提示"; //邮件标题
            String text = ""; //邮件内容

            text = "用户IP："+userInfo.getIp()+"访问了["+userInfo.getSystemName()+"]，归属地："
                    +userInfo.getRegionName()+" "+userInfo.getCity();
            send.sendQQEmail(subject,text);*/
            return "操作成功";
        }
        return "操作失败";
    }

    @Override
    public ResponseParam findList(UserInfoReq req) {
        Page<UserInfo> page = new Page<>(req.getCurrent(), req.getSize());

        QueryWrapper wrapper = new QueryWrapper();

        wrapper.orderByDesc("id");
        ResponseParam responseParam = new ResponseParam();
        responseParam.setData(userInfoMapper.selectPage(page,wrapper));
        return responseParam;
    }
}
